package package1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestForXMLParse {

	public static void main(String[] args) throws ParserConfigurationException, FileNotFoundException, SAXException, IOException, DocumentException {
//		parseXMLBuilderFactory();
		parseXMLDom4j();
	}
	private static void parseXMLDom4j() throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File(getDesktop()+"2.xml")); 
		org.dom4j.Element root = document.getRootElement();
		List<List<String>> allList  = new ArrayList<List<String>>();
		List<Element> list = root.elements();
		
	}
	private static void parseXMLBuilderFactory() throws FileNotFoundException, SAXException, IOException, ParserConfigurationException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
		DocumentBuilder builder = dbf.newDocumentBuilder();  
        org.w3c.dom.Document doc = builder.parse((new FileInputStream(new File(getDesktop()+"2.xml"))));
        org.w3c.dom.Element root = doc.getDocumentElement();
        parseRootNode(root,0);
		System.err.println("over");
	}
	private static void parseRootNode(Element root,int layer) {
		System.err.println("******");
		String nodeName = root.getNodeName();
		print("nodeName",nodeName,layer);
		String nodeValue = root.getNodeValue();
		print("nodeValue",nodeValue,layer);
		NamedNodeMap attributes = root.getAttributes();
		for(int i=0;i<attributes.getLength();i++){
			Node attribute = attributes.item(i);
			print("nodeAttribute",attribute,layer);
		}
        NodeList children = root.getChildNodes();
        if(children.getLength()==0)
        	return;
        for(int i=0;i<children.getLength();i++){
        	Node item = children.item(i);
        	parseNode(item,layer+1);
        }
	}
	private static void parseNode(Node item, int layer) {
		System.err.println("******");
		String nodeName = item.getNodeName();
		print("nodeName",nodeName,layer);
		String nodeValue = item.getNodeValue();
		print("nodeValue",nodeValue,layer);
		NamedNodeMap attributes = item.getAttributes();
		if(attributes==null||attributes.getLength()==0)
			return;
		for(int i=0;i<attributes.getLength();i++){
			Node attribute = attributes.item(i);
			print("nodeAttribute",attribute,layer);
		}
        NodeList children = item.getChildNodes();
        for(int i=0;i<children.getLength();i++){
        	Node item2 = children.item(i);
        	parseNode(item2,layer+1);
        }
	}
	private static void print(String name, Node attribute, int layer) {
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<layer;i++){
			sb.append("--|");
		}
		System.err.println(sb+name+":"+(attribute==null?null:attribute));
	}
	private static void print(String name, String value,int layer) {
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<layer;i++){
			sb.append("--|");
		}
		System.err.println(sb+name+":"+(value==null?null:value));
	}
	private static String getDesktop() {
		return System.getenv("USERPROFILE") + "\\desktop\\";
	}
}
